﻿using HiShing.TongDanGuanJia.Devices;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

namespace HiShing.TongDanGuanJia.EntityFrameworkCore.Seed.UserSettings
{
    public class DefaultDeviceTypeCreator
    {
        public static List<DeviceType> InitialDeviceTypes => GetInitialDeviceTypes();
        private readonly TongDanGuanJiaDbContext _context;

        public DefaultDeviceTypeCreator(TongDanGuanJiaDbContext context)
        {
            _context = context;
        }
        public void Create()
        {
            foreach (var type in InitialDeviceTypes)
            {
                AddDeviceTypeIfNotExists(type);
            }
        }
        private void AddDeviceTypeIfNotExists(DeviceType deviceType)
        {
            if (_context.DeviceType.IgnoreQueryFilters().Any(s => s.DisplayName == deviceType.DisplayName))
            {
                return;
            }

            _context.DeviceType.Add(deviceType);
            _context.SaveChanges();
        }

        private static List<DeviceType> GetInitialDeviceTypes()
        {
            return new List<DeviceType>
            {
                new DeviceType(){ DisplayName="数蛋器", Id=15},
                new DeviceType(){ DisplayName="激光数蛋器", Id=27},
            };
        }
    }
}
